package org.wukongcrm.domain.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.wukongcrm.domain.entity.AdminUser;
import org.wukongcrm.domain.entity.Menu;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 登录用户
 *
 * @author ZhongWang
 * @since 2024 12 31:09:30
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginUser implements UserDetails {

    private AdminUser users;

    private List<Menu> permissions;

    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities () {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Menu permission : permissions) {
            authorities.add(new SimpleGrantedAuthority(permission.getPerm()));
        }
        return authorities;
    }

    @Override
    @JsonIgnore
    public String getPassword () {
        return users.getPassword();
    }

    @JsonIgnore
    @Override
    public String getUsername () {
        return users.getUsername();
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired () {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked () {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired () {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled () {
        return true;
    }
}
